//
//  LYUserManager.swift
//  WeChat
//
//  Created by mac on 2018/8/14.
//  Copyright © 2018年 RRC. All rights reserved.
//

import Foundation
import KeychainAccess
import SwiftyJSON

let UserInstance = UserManager.sharedInstance

private let kNickname = "kLY_wechat_username"
private let kAvatar = "kLY_wechat_avatar"
private let kAccessToken = "kLY_wechat_accessToken"
private let kUserId = "kLY_wechat_userId"
private let kIsLogin = "kLY_wechat_isLogin"
private let kLoginName = "kLY_wecaht_loginName"
private let kPassword = "kLY_wechat_password"

class UserManager: NSObject {
    class var sharedInstance : UserManager {
        struct Static {
            static let instance : UserManager = UserManager()
        }
        return Static.instance
    }
    
    //keychain
    let LYKeychain = Keychain(service: "com.wechat.Hilen")
    
    var accessToken: String? {
        get {
            return UserDefaults.ts_stringForKey(kAccessToken, defaultValue: "这是我的 AccessToken")
        }
        set {
          UserDefaults.ts_setString(kAccessToken, value: newValue)
        }
    }
    
    /// 用户昵称，不是登录名
    var nickname: String? {
        get {
            return UserDefaults.ts_stringForKey(kNickname, defaultValue: "")
        }
        set {
            UserDefaults.ts_setString(kNickname, value: newValue)
        }
    }
    
    var avatar: String?
    var userId: String? {
        get {
            return UserDefaults.ts_stringForKey(kUserId, defaultValue: LYConfig.testUserId)
        }
        set {
            UserDefaults.ts_setString(kUserId, value: newValue)
        }
    }
    
    var isLogin: Bool {
        get {
            return UserDefaults.ts_boolForKey(kIsLogin, defaultValue: false)
        }
        set {
            UserDefaults.ts_setBool(kIsLogin, value: newValue)
        }
    }
    
    /// 用户手机号
    var loginName: String? {
        get {
            return LYKeychain[kLoginName] ?? ""
        }
        set {
            LYKeychain[kLoginName] = newValue
        }
    }
    
    /// 密码，存在 keychain
    var password:String? {
        get {
            return LYKeychain[kPassword] ?? ""
        }
        set {
            LYKeychain[kPassword] = newValue
        }
    }
    
    func readAllData() {
        self.nickname = UserDefaults.ts_stringForKey(kNickname, defaultValue: "")
        self.avatar = UserDefaults.ts_stringForKey(kAvatar, defaultValue: "")
        self.userId = UserDefaults.ts_stringForKey(kUserId, defaultValue: "")
        self.isLogin = UserDefaults.ts_boolForKey(kIsLogin, defaultValue: false)
        self.loginName = LYKeychain[kLoginName] ?? ""
        self.password = LYKeychain[kPassword] ?? ""
    }
    
    
    /// 登录成功
    ///
    /// - Parameter result: 登录成功后传进来的字典
    func userLoginSuccess(_ result: JSON) {
        self.loginName = result["username"].stringValue
        self.password = result["password"].stringValue
        self.nickname = result["nickname"].stringValue
        self.userId = result["user_id"].stringValue
        self.isLogin = true
    }
    
    
    /// 退出登录
    func userLogout() {
        self.accessToken = ""
        self.loginName = ""
        self.password = ""
        self.nickname = ""
        self.userId = ""
        self.isLogin = false
    }
    
    func resetAccessToken(_ token:String) {
        UserDefaults.ts_setString(kAccessToken, value: token)
        if token.count > 0 {
            print("token success")
        }else{
            self.userLogout()
        }
    }
    
}

